package com.zx.cloud.common.auth0.jwt;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.zx.cloud.common.auth0.jwt.algorithms.Algorithm;
import com.zx.cloud.common.auth0.jwt.exceptions.InvalidClaimException;
import com.zx.cloud.common.auth0.jwt.exceptions.JWTCreationException;
import com.zx.cloud.common.auth0.jwt.exceptions.JWTVerificationException;
import com.zx.cloud.common.auth0.jwt.interfaces.Claim;
import com.zx.cloud.common.filter.MagicConstants;
import com.zx.cloud.util.DateTimeUtil;

public class TokenGenerator {

    private static final String secret = "secret";
    private static final String issuer = "issuer";
    private static final int expireHours = 7*24;//30天过期
    
    public static void main(String[] args) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
    	Map<String, Object> claims=new HashMap<String, Object>();
    	claims.put(MagicConstants.SESSION_USER_ID, 1);
    	Token token=TokenGenerator.create(claims);
    	System.out.println(token.getToken());
	}
    

    public static Token create(Map<String, Object> claims) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
        String tokenString = JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addHours(DateTimeUtil.getSystemDate(), expireHours))
                .withClaim(claims).sign(Algorithm.HMAC256(secret));
        return new Token(tokenString, expireHours * 60* 60);
    }

    public static String create(int expiresAt, Map<String, Object> claims) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
        return JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addSeconds(DateTimeUtil.getSystemDate(), expiresAt))
                .withClaim(claims).sign(Algorithm.HMAC256(secret));
    }

    public static Claim getClaim(String token, String claim) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
        return JWT.decode(token).getClaim(claim);
    }

    public static Map<String, Claim> getClaims(String token) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
        return JWT.decode(token).getClaims();
    }

    public static boolean isExpired(String token) throws JWTVerificationException, IllegalArgumentException, UnsupportedEncodingException {
        try {
            return DateTimeUtil.compareTwoDate(DateTimeUtil.getSystemDate(), JWT.require(Algorithm.HMAC256(secret)).withIssuer(issuer).build().verify(token).getExpiresAt()) < 0 ? true : false;
        } catch (InvalidClaimException e) {
            return false;
        }
    }

    public static String create(String secret, String issuer, int expiresAt, Map<String, Object> claims) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
        return JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addSeconds(DateTimeUtil.getSystemDate(), expiresAt))
                .withClaim(claims).sign(Algorithm.HMAC256(secret));
    }

    public static boolean isExpired(String secret, String issuer, String token) throws JWTVerificationException, IllegalArgumentException, UnsupportedEncodingException {
        return DateTimeUtil.compareTwoDate(DateTimeUtil.getSystemDate(), JWT.require(Algorithm.HMAC256(secret)).withIssuer(issuer).build().verify(token).getExpiresAt()) > 0 ? true : false;
    }

}
